<?php

namespace App\Http\ViewComposers;

use Illuminate\Contracts\View\View;
use App\Models\SiteMenu;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;

class AdminComposer
{
    /**
     * 栏目列表.
     * @var SiteMenu
     */
    protected $site_menu=array();

    protected $child_menu=array();

    protected $menu_keys = '';

    protected $permsids = array();

    protected $requestUri;

    protected $baidukey;

    protected $authUser;
    /**
     * Create a new profile composer.
     *
     * @param  UserRepository  $users
     * @return void
     */
    public function __construct()
    {
        // Dependencies automatically resolved by service container...
        $this->requestUri = Request::getRequestUri();
        $this->formatUriKey();
//         $perms = Auth::user()->role;
        $roles = Auth::user()->roles;
        $perms = array();
        foreach ($roles as $role)
        {
            $perms[] = $role->perms;
        }
        foreach ($perms as $perm)
        {
            foreach ($perm as $p)
            {
                $this->permsids[]=$p->id;
            }
        }
        $this->permsids=array_unique($this->permsids);
        $this->site_menu = SiteMenu::where('parent_id','=',0)->whereIn('permission_id',$this->permsids)->orderBy('display_order','ASC')->orderBy('menu_id','ASC')->get(); //

        foreach ($this->site_menu as $menu)
        {
            $this->child_menu[$menu->description] = SiteMenu::where('parent_id','=',$menu->menu_id)
                                                    ->orderBy('display_order','ASC')
                                                    ->orderBy('menu_id','ASC')
                                                    ->get();
            foreach ($this->child_menu[$menu->description] as &$chmenu) 
            {
                $chmenu[$chmenu->description] = SiteMenu::where('parent_id','=',$chmenu->menu_id)
                                                ->whereIn('permission_id',$this->permsids)
                                                ->orderBy('display_order','ASC')
                                                ->orderBy('menu_id','ASC')
                                                ->get();
            }
        }
        $this->baidukey = Config::get('app.baidukey');
        $this->authUser = Auth::user();
    }

    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $view->with('site_menu', $this->site_menu);
        $view->with('child_menu',$this->child_menu);
        $view->with('request_uri',$this->requestUri);
        $view->with('menu_keys',$this->menu_keys);
        $view->with('baidukey',$this->baidukey);
        $view->with('authuser',$this->authUser);
    }

    private function formatUriKey(){
        if(str_contains( $this->requestUri , 'admin/index') !== false || str_contains( $this->requestUri , 'admin/employment') !== false)
        {
            $this->menu_keys = 'index';
        }
        elseif (str_contains( $this->requestUri , 'admin/employ') !== false)
        {
            $this->menu_keys = 'employ';
        }
        elseif (str_contains( $this->requestUri , 'admin/teaches') !== false)
        {
            $this->menu_keys = 'teaches';
        }
        elseif (str_contains( $this->requestUri , 'admin/enterprise') !== false)
        {
            $this->menu_keys = 'enterprise';
        }
        elseif (str_contains( $this->requestUri , 'admin/bbs') !== false)
        {
            $this->menu_keys = 'bbs';
        }
        elseif (str_contains( $this->requestUri , 'admin/users') !== false)
        {
            $this->menu_keys = 'users';
        }
        elseif (str_contains( $this->requestUri , 'admin/content') !== false)
        {
            $this->menu_keys = 'content';
        }
        elseif (str_contains( $this->requestUri , 'admin/setting') !== false)
        {
            $this->menu_keys = 'setting';
        }
    }
}